<?php
 session_start();
 include("include/session.php");
 include("include/common.php");
 include("include/db.php");

 
 if(isset($_REQUEST) && $_REQUEST['submit'] != ''){
 	
	$user_type 		= mysql_real_escape_string($_REQUEST['user_type']); 
	$user_course	= $_REQUEST['user_course'];

	$user_email 	= mysql_real_escape_string($_REQUEST['user_email']);
	$user_password 	= mysql_real_escape_string($_REQUEST['user_password']);
	$first_name 	= mysql_real_escape_string($_REQUEST['first_name']);
	$last_name 		= mysql_real_escape_string($_REQUEST['last_name']);
	$user_mobile 	= mysql_real_escape_string($_REQUEST['user_mobile']);
	$user_address 	= mysql_real_escape_string($_REQUEST['user_address']);
	$user_city 		= mysql_real_escape_string($_REQUEST['user_city']);
	$user_state 	= mysql_real_escape_string($_REQUEST['user_state']);
	$user_country 	= mysql_real_escape_string($_REQUEST['user_country']);
	$user_comments 	= mysql_real_escape_string($_REQUEST['user_comments']);
	$active 		= mysql_real_escape_string($_REQUEST['active']);
	
	
	$sqlCategory = "UPDATE software_user
								 SET 
							email					=	'$user_email',
							password				=	'$user_password',
							address					=	'$user_address',
							city					=	'$user_city',
							state					=	'$user_state',
							country					=	'$user_country',
							user_type				=	'$user_type',
							user_course				=	'$user_course',
							user_creation_date		=	UNIX_TIMESTAMP(),
							user_expirey_date		=	'$user_email',
							active					=	'$active',
							user_comments			=	'$user_comments',
							first_name				=	'$first_name',
							last_name				=	'$last_name',
							mobile					=	'$user_mobile'
						WHERE 
							id = '".$_REQUEST['user_id']."'
							"; 	
							

 	if(mysql_query($sqlCategory))
	{
		if(!empty($user_course))
		{
			mysql_query("DELETE FROM software_user_courses WHERE user_id = '".$_REQUEST['user_id']."'");
			foreach($user_course as $courses){
				$course			 = "INSERT INTO software_user_courses SET user_course = '$courses',user_type = '$user_type',
											user_id = '".$_REQUEST['user_id']."',course_date=UNIX_TIMESTAMP()";
				$userCourseAdded = mysql_query($course);	
			}
		}
		
		header("location:edituser.php?msg=1&user_id=".$_REQUEST['user_id']);
	
	}
	else
		header("location:edituser.php?msg=2&user_id=".$_REQUEST['user_id']);
	exit();		
 }
 
  $sqlUser = mysql_query("SELECT * FROM software_user 
 										WHERE id = '".intval($_REQUEST['user_id'])."'");
  $resUser  = mysql_fetch_array($sqlUser);
  
  $sqlUserCourses = mysql_query("SELECT * FROM software_user_courses WHERE user_id = '".intval($_REQUEST['user_id'])."'");
  while($resUserCourses = mysql_fetch_array($sqlUserCourses)){
		$arrUserCourses[]	= $resUserCourses["user_course"];		  
  }
  

 ?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?=TITLE_WEBSITE?></title>
<link href="css/c_energy.css" rel="stylesheet" type="text/css" />
<link href="css/logo.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="css/datepicker.css" type="text/css" />
<link rel="stylesheet" media="screen" type="text/css" href="css/layout.css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/datepicker.js"></script>
<script type="text/javascript" src="js/eye.js"></script>
<script type="text/javascript" src="js/utils.js"></script>
<script type="text/javascript" src="js/layout.js?ver=1.0.2"></script>
<script type="text/javascript">
</script>
<style type="text/css">
<!--
.style4 {
	font-size: 22px;
	font-weight: bold;
}
.style6 {color: #999999}
-->
</style>
</head>
<script >
$('#inputDate').DatePicker({
	format:'m/d/Y',
	date: $('#inputDate').val(),
	current: $('#inputDate').val(),
	starts: 1,
	position: 'r',
	onBeforeShow: function(){
		$('#inputDate').DatePickerSetDate($('#inputDate').val(), true);
	},
	onChange: function(formated, dates){
		$('#inputDate').val(formated);
		if ($('#closeOnSelect input').attr('checked')) {
			$('#inputDate').DatePickerHide();
		}
	}
});
function validation(){
	
	var message = '';
	
	
	if(document.getElementById("first_name").value == ""){
		message += 'Please enter your first name\n';
	}
	if(document.getElementById("last_name").value == ""){
		message += 'Please enter your last name\n';
	}
	if(document.getElementById("user_email").value == ""){
		message += 'Please enter your user email\n';
	}
	if(document.getElementById("user_email").value != ""){
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    	var address = document.forms['signform'].elements['user_email'].value;
    	if(reg.test(address) == false) {
 			message = 'Invalid Email Address\n';
      	}
	}
	if(document.getElementById("user_mobile").value == ""){
		message += 'Please enter your mobile';
	}
	
	if(message != ""){
		alert(message);
		return false;
	}
	
	
}
</script>
<body>

    <table width="990" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
        <td colspan="3" align="left" valign="top" ><? require("header.php");?></td>
</tr>
  <tr>
    <td align="center" >
	<table width="99%" cellspacing="0" cellpadding="0">
      <tr>
        <td colspan="3" align="center" valign="top">&nbsp;</td>
      </tr>
       <tr bgcolor="#FAFAFA">
        <td colspan="0" align="left" valign="top" >
		   <table width="96%" cellspacing="0" cellpadding="0">
		    <tr>
		      <td width="2%" align="left" valign="top" style="padding-left:15px" ><? require("left.php");?></td>
		      <td width="94%"  valign="top" style="padding-left:15px" >
			  <form name="signform" action="" method="post" enctype="multipart/form-data" id="signform" onsubmit="return validation();" >
			    <table id="signInTbl" width="719" cellspacing="0" cellpadding="0" style="border:solid #cccccc 1px">
                  <tr>
                    <td colspan="2" align="left" bgcolor="#373737" class="loc_search_found_term"  style="border-bottom:solid #cccccc 1px">Add/ Edit Pages </td>
                  </tr>
                  <?php
                  	if($_REQUEST['msg'] == 1)
					{
				  ?>
                  <tr style="background: #D5FFD5 " height="25">
                 	<td colspan="2"><div align="left" class="successMsg" id="err_site" style="display:block">
                       &nbsp; <strong><?='User updated successfully'?></strong>
                    </div></td>
                  </tr>
                  <?php
                  	}elseif($_REQUEST['msg'] == 2){
				  ?>
                  <tr style="background: #FFE9E9 " height="25">
                 	<td colspan="2"><div align="left" class="ErrorMsg" id="err_site" style="display:block">
                       &nbsp; <strong><?='Sorry,record does not inserted.'?></strong>
                    </div></td>
                  </tr>	
				  <?php		
					}
				  ?>
                 <tr height="30">
                    <td width="249" align="left" style="padding-left:15px">User Type</td>
                    <td width="466" align="left">
                    	<select id="user_type" name="user_type" style="width:220px" >
                        	<option value="" >Select</option>
                            <option value="Student" <?=("Student" == $resUser['user_type']  ? 'selected=selected' : '')?>>Student</option>
                            <option value="Trainer" <?=("Trainer" == $resUser['user_type']  ? 'selected=selected' : '')?>>Trainer</option>
                        </select>
                    </td>
                </tr>
                <tr height="30">
                    <td width="249" align="left" style="padding-left:15px">User Course</td>
                    <td width="466" align="left">
                    	<select id="user_course[]" name="user_course[]" style="width:220px" multiple="multiple" >
                       	   <?php
						    $sqlCourse = mysql_query("SELECT * FROM software_main_course where active = 'Y'");
							while($resCourse = mysql_fetch_array($sqlCourse)){
						   ?>
                           	<option value="<?php echo $resCourse["course_name"]?>" 
                          		<?php
                                	if(!empty($arrUserCourses))
									{
										if(in_array($resCourse["course_name"],$arrUserCourses)){
											echo "selected=selected";	
										}else{
											echo "";	
										}
									}
								?>  
                           ><?php echo $resCourse["course_name"]?></option>
                           
						   <?php			
							}
						   ?>
                        </select>
                    </td>
                </tr> 
                 <tr height="30">
                    <td width="249" align="left" style="padding-left:15px">Email</td>
                    <td width="466" align="left">
                      <input  style="width:220px" name="user_email" id="user_email" type="text" class="Box" maxlength="35"  value="<?=$resUser['email']?>" />                    </td>
                </tr>
                <tr height="30">
                    <td width="249" align="left" style="padding-left:15px"> Password</td>
                    <td width="466" align="left">
                      <input  style="width:220px" name="user_password" id="user_password" type="password" class="Box" maxlength="35"  value="<?=$resUser['password']?>" />                    </td>
                </tr>
				<tr height="30">
                    <td width="249" align="left" style="padding-left:15px">First Name</td>
                    <td width="466" align="left">
                      <input  style="width:220px" name="first_name" id="first_name" type="text" class="Box"  maxlength="25" value="<?=$resUser['first_name']?>" />                    </td>
                  </tr>
                   <tr height="30">
                    <td width="249" align="left" style="padding-left:15px">Last Name</td>
                    <td width="466" align="left">
                      <input  style="width:220px" name="last_name" id="last_name" type="text" class="Box"  maxlength="25" value="<?=$resUser['last_name']?>" />                    </td>
                  </tr>
                  
                   <tr height="30">
                    <td width="249" align="left" style="padding-left:15px">Mobile</td>
                    <td width="466" align="left">
                      <input  style="width:220px" name="user_mobile" id="user_mobile" type="text" class="Box"  maxlength="10" value="<?=$resUser['mobile']?>" />                    </td>
                  </tr>
                   <tr height="30">
                    <td width="249" align="left" style="padding-left:15px">Address</td>
                    <td width="466" align="left">
                      <input  style="width:220px" name="user_address" id="user_address" type="text" class="Box" value="<?=$resUser['address']?>" maxlength="70" />                    </td>
                  </tr>
                   <tr height="30">
                    <td width="249" align="left" style="padding-left:15px">City</td>
                    <td width="466" align="left">
                      <input  style="width:220px" name="user_city" id="user_city" type="text" class="Box" value="<?=$resUser['city']?>" maxlength="70" />                    </td>
                  </tr>
                   <tr height="30">
                    <td width="249" align="left" style="padding-left:15px">State</td>
                    <td width="466" align="left">
                      <input  style="width:220px" name="user_state" id="user_state" type="text" class="Box" value="<?=$resUser['state']?>" maxlength="70" />                    </td>
                  </tr>
                   <tr height="30">
                    <td width="249" align="left" style="padding-left:15px">Country</td>
                    <td width="466" align="left">
                    	<select id="user_country" name="user_country" style="width:220px" >
                        	<option value="" >Select</option>
							<?php
                            	$sqlCountry = mysql_query("SELECT * FROM software_country WHERE status = 'Y'");
								while($resCountry = mysql_fetch_array($sqlCountry)){
							?>
                            <option value="<?php echo $resCountry["country_code"]; ?>" <?=($resCountry["country_code"] == $resUser['country'] ? 'selected=selected' : '')?> ><?php echo $resCountry["country_name"]; ?></option>
                            <?php
								}
							?>
                        </select>
                    </td>
                  </tr>
                  <tr height="30">
                    <td width="249" align="left" style="padding-left:15px">Comments</td>
                    <td width="466" align="left">
                      <input  style="width:220px" name="user_comments" id="user_comments" type="text" class="Box" value="<?=$resUser['user_comments']?>" maxlength="70" />                    </td>
                  </tr>
                  <tr  height="30">
                    <td width="249" align="left" style="padding-left:15px">Active</td>
                    <td width="466" align="left">
                    	<select id="active" name="active" style="width:220px" >
                        	<option value="" >Select</option>
                            <option value="N" <?=($resUser["active"] == 'N' ? 'selected=selected' : '')?>>Non-Active</option>
                            <option value="Y" <?=($resUser["active"] == 'Y' ? 'selected=selected' : '')?>>Active</option>
                        </select>                    </td>
                  </tr>
                   <tr  height="30">
                    <td width="249"  style="padding-left:15px" colspan="2" align="center"><input class="cls_button" type="submit" id="submit" name="submit" value="Submit"  /></td>
                  </tr>
                </table>
			    <ul class="site_nav_menu_buttons">
			      <li class="no_menu_li"><span><a href="user.php">&nbsp;Back to User </a></span></li>
			      </ul>
			    <p>&nbsp;</p>
			  </form>			  </td>
            </tr>
	        </table></td>
        </tr>
      <tr bgcolor="#FAFAFA">
        <td width="800" align="center" valign="top">&nbsp;</td>
        <td width="100" colspan="2">&nbsp;</td>
      </tr>
	    <tr bgcolor="#FAFAFA">
        <td width="800" align="center" valign="top">&nbsp;</td>
        <td colspan="2">&nbsp;</td>
      </tr>
    </table></td>
  </tr>
</table>
</body>
</html>
